<?php

// $Id: subjectmanagement.php, 2010/10/04 afterlastangel Exp $

/**
 * This page lists all the instances of studentcp in a particular course
 *
 * @author  Le Kien Truc <afterlastangel@gmail.com>
 * @version $Id: subjectmanagement.php, 2010/10/04 $
 * @package mod/studentcp
 */
/// (Replace studentcp with the name of your module and remove this line)

require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
require_once(dirname(__FILE__) . '/locallib.php');
require_once(dirname(__FILE__) . '/subject/subject_form.php');

$id = optional_param('id', 0, PARAM_INT); // course_module ID, or
$a = optional_param('a', 0, PARAM_INT);  // studentcp instance ID
$subjectid = optional_param('subjectid', -1, PARAM_INT);  // studentcp instance ID
$mode = optional_param('mode', '', PARAM_TAG);

if ($id) {
    if (!$cm = get_coursemodule_from_id('studentcp', $id)) {
        error('Course Module ID was incorrect');
    }

    if (!$course = get_record('course', 'id', $cm->course)) {
        error('Course is misconfigured');
    }

    if (!$studentcp = get_record('studentcp', 'id', $cm->instance)) {
        error('Course module is incorrect');
    }
} else if ($a) {
    if (!$studentcp = get_record('studentcp', 'id', $a)) {
        error('Course module is incorrect');
    }
    if (!$course = get_record('course', 'id', $studentcp->course)) {
        error('Course is misconfigured');
    }
    if (!$cm = get_coursemodule_from_instance('studentcp', $studentcp->id, $course->id)) {
        error('Course Module ID was incorrect');
    }
} else {
    error('You must specify a course_module ID or an instance ID');
}

require_login($course, true, $cm);
if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
    print_error('badcontext');
}
require_capability('mod/studentcp:managesubject', $context);

add_to_log($course->id, "studentcp", "view", "view.php?id=$cm->id", "$studentcp->id");

/// Print the page header
$strstudentcps = get_string('modulenameplural', 'studentcp');
$strstudentcp = get_string('modulename', 'studentcp');

$navlinks = array();
$navlinks[] = array('name' => $strstudentcps, 'link' => "index.php?id=$course->id", 'type' => 'activity');
$navlinks[] = array('name' => format_string($studentcp->name), 'link' => '', 'type' => 'activityinstance');

$navigation = build_navigation($navlinks);

print_header_simple(format_string(get_string('subjectmanagement', 'studentcp')), '', $navigation, '', '', true,
        update_module_button($cm->id, $course->id, $strstudentcp), navmenu($course, $cm));

/// Print the main part of the page
show_tabs($cm, $context, 'subjectmanagement');
$maxuploadsize = get_max_upload_file_size();
$mform_export = new mod_studentcp_subject_form("subject/subjectupdate.php?id=$id", array('course' => $course, 'cm' => $cm, 'modcontext' => $context, 'subjectid' => $subjectid, 'maxuploadsize' => $maxuploadsize));
if ($mode == 'edit' || $subjectid==0) {
    $mform_export->display();
}
if ($mode == 'view') {
    print_grade_report($subjectid, $cm);
}
//Print the subject table
//developing category
//$categoryid = 1;
if ($mode == '') {
    echo "<center><a href=\"./subjectmanagement.php?id={$cm->id}&subjectid=0\">" . get_string('addnewsubject', 'studentcp') . "</a></center>";

    unset($table); {
        $table->head[] = get_string('subjectcode', 'studentcp');
        $table->align[] = 'center';
        $table->size[] = '';

        $table->head[] = get_string('subjectname', 'studentcp');
        $table->align[] = 'center';
        $table->size[] = '';

        $table->head[] = get_string('category', 'studentcp');
        $table->align[] = 'center';
        $table->size[] = '';

        $table->head[] = get_string('credit', 'studentcp');
        $table->align[] = 'center';
        $table->size[] = '';

        $table->head[] = get_string('description', 'studentcp');
        $table->align[] = 'center';
        $table->size[] = '';

        $table->head[] = get_string('gradedesc', 'studentcp');
        $table->align[] = 'center';
        $table->size[] = '';

        $table->head[] = get_string('gradetable', 'studentcp');
        $table->align[] = 'center';
        $table->size[] = '';
    }
    if ($categoryid)
        $subjects = get_records('studentcp_subject', 'categoryid', $categoryid);
    else
        $subjects = get_records('studentcp_subject');

    $subjectnum = 0;
    foreach ($subjects as $subject) {
        $table->data[$subjectnum][] = $subject->subjectcode;
        $table->data[$subjectnum][] = "<a href=\"subjectmanagement.php?id={$cm->id}&subjectid={$subject->id}&mode=edit\">" . $subject->name . "</a>";
        $table->data[$subjectnum][] = $subject->categoryid;
        $table->data[$subjectnum][] = $subject->credit;
        $table->data[$subjectnum][] = $subject->description;
        $table->data[$subjectnum][] = $subject->gradedesc;
        $table->data[$subjectnum][] = $subject->gradetable;

        $subjectnum++;
    }
    print_table($table);
}



/// Finish the page
print_footer($course);
?>
